package com.example.finallyjob.Controllers;

import com.example.finallyjob.entity.Info;
import com.example.finallyjob.entity.User;
import com.example.finallyjob.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Api(description = "用户功能")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "登录",notes = "返回用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password", value = "用户密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "status", value = "用户登录状态码(1表示用户登录||10表示管理员登录)", required = true, dataType = "String")
    })
    @PostMapping("/loginin")
    public User login(@RequestParam("name") String name,
                      @RequestParam("password") String password,
                      @RequestParam("status")String status) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return userService.login(name,password,status);
    }

    @ApiOperation(value = "注册",notes = "返回用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password", value = "用户密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "status", value = "用户注册状态码(1表示用户注册||0表示管理员注册)", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "Img", value = "图片的编号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "label", value = "用户简介", required = false, dataType = "String")
    })
    @PostMapping("/registerin")
    public User register(String name,String password, String status,
                         @RequestParam(required = false) String Img,
                         @RequestParam(required = false)String label) throws Exception {
        return userService.register(name,password,
                status,Img,label);
    }

    @ApiOperation("通过管理员申请")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "param", name = "id", value = "需通过申请的管理员id号", required = true, dataType = "Int")
    })
    @PutMapping("/agree/{id}")
    public Boolean agree(@PathVariable("id") int id){
        return userService.agree(id);
    }

    @ApiOperation(value = "修改个人头像和简介信息",notes = "返回更新后的个人头像简介信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "用户名", required = true, dataType = "Int"),
            @ApiImplicitParam(paramType = "query", name = "status", value = "用户状态码(1表示用户||10表示管理员)", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "Img", value = "图片的编号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "label", value = "用户简介", required = false, dataType = "String")
    })
    @PutMapping("/updata")
    public User updata(String status, int id,
                       @RequestParam(required = false) String Img,
                       @RequestParam(required = false) String label){
        return userService.updata(status,id,Img,label);
    }

    @ApiOperation(value = "查询用户收到的消息",notes = "返回消息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "用户id", required = true, dataType = "Int")
    })
    @GetMapping("/findInfo/{id}")
    public List<Info> FindInfo(@PathVariable int id){
        return userService.FindInfo(id);
    }


    @ApiOperation(value = "用户删除自己的作品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "用户id", required = true, dataType = "Int"),
            @ApiImplicitParam(paramType = "query", name = "status", value = "作品类型（text||video）", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "num", value = "作品文件编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "userText", value = "用户文章列表", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "userVideo", value = "用户视频列表", required = false, dataType = "String")

    })
    @DeleteMapping("/delete")
    public String delete(String num, int id,
                         String status,
                         @RequestParam(required = false) String userText,
                         @RequestParam(required = false)String userVideo) throws Exception {
        String i=userService.UserDelect(num,id,status,userText,userVideo);
        return i;
    }


    @ApiOperation(value = "管理员删除作品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "作者id", required = true, dataType = "Int"),
            @ApiImplicitParam(paramType = "query", name = "status", value = "作品类型（text||video）", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "num", value = "作品文件编号", required = true, dataType = "String")
    })
    @DeleteMapping("/adDelete")
    public String adDelete(String num, int id, String status) throws Exception {
        String i=userService.adminDelect(num, id, status);
        return i;
    }
}
